package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.gson.crm.model.dao.InveDao;
import cn.gson.crm.model.dao.InveinfoDao;
import cn.gson.crm.model.dao.ProductDao;
import cn.gson.crm.model.mapper.InveMapper;
import cn.gson.crm.model.mapper.ProductMapper;
import cn.gson.crm.model.pojo.InveInfoTb;
import cn.gson.crm.model.pojo.InveTb;
import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.vo.InveInfoTbVo;
import cn.gson.crm.vo.InveTbVo;
import cn.gson.crm.vo.ProductVo;

@Service
@Transactional
public class InveService {

	@Autowired
	private ProductMapper mapp;
	@Autowired
	private ProductDao proDAO;
	@Autowired
	private InveinfoDao inveinfoDAO;
	@Autowired
	private InveMapper invemapper;
	public List<InveTbVo> getAllinveBypage(){
		try {
			
			/*PageRequest p = new PageRequest(page - 1, rows);*/
	/*		List<Object[] >list=inveDAO.queryByPage(p);
			for(Object[] o:list){
				InveTb inv=(InveTb)o[0];
				Product orp=(Product)o[1];
				System.out.println(inv);
				System.out.println(orp);
			}*/
	List<InveTb> listPo=invemapper.findAllin();

	
	  List<InveTbVo> listVo=Po2VoList(listPo);
			return listVo;
		
		} catch (Exception e) {
				e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	public List<InveInfoTbVo>getAllInveinfos(int page,int rows){
		
			PageRequest p = new PageRequest(page - 1, rows);// 第一个参数是第几页，第二个参数是多少个
			List<InveInfoTb> listPo = inveinfoDAO.queryByPage(p);
			List<InveInfoTbVo> listVo = Po3VoList(listPo);
			return listVo;
	
	}
public List <InveInfoTbVo>getAllInveinfosBykey(int page,int rows,Long key){
	PageRequest p=new PageRequest(page-1,rows);
	List<InveInfoTb> listPo=inveinfoDAO.queryByPageaa(key,p);
	List<InveInfoTbVo>listVo=Po3VoList(listPo);
	return listVo;
}
	
	private List<InveTbVo>Po2VoList(List<InveTb> listproPo){
		List<InveTbVo>listproVo=new ArrayList<>();
		//listproPo中只存有id 数量 还有一个product对象
		if(listproPo !=null){
			for(int i=0;i<listproPo.size();i++){
				InveTbVo lisVo=new InveTbVo(listproPo.get(i).getInveId(), listproPo.get(i).getInveProNumb(), listproPo.get(i).getProducts().get(0).getProInfoSta(), listproPo.get(i).getProducts().get(0).getProductName());
				listproVo.add(lisVo);
			}
		}
		return listproVo;
	}
	
	
	private List<InveInfoTbVo>Po3VoList(List<InveInfoTb> listproPo){
		List<InveInfoTbVo>listingoVo=new ArrayList<>();
		if(listproPo!=null){
	
			for(InveInfoTb p:listproPo){
			
				InveInfoTbVo inveinfoVo=new InveInfoTbVo(p.getInveInfoId(), p.getWarehouse(), p.getInveInfoTime(), p.getInveEmpId(), p.getInveInfoType(),p.getInveinfonumb());
				listingoVo.add(inveinfoVo);
			}
		}
		return listingoVo;
	}
	
	
	public long count() {
		return proDAO.count();
	}
	
	public long count1() {
		return inveinfoDAO.count();
	}
	public long countwhere(Long key){
		return inveinfoDAO.countwhere(key);
	}
}
